package com.tfswx.jcyw.dmzc.action;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.tfswx.jcyw.dmzc.config.CommonData;
import com.tfswx.jcyw.dmzc.util.ImageBuilder;
import com.tfswx.jcyw.dmzc.util.NotificationUtil;
import com.tfswx.jcyw.dmzc.util.ProjectUtil;
import com.tfswx.jcyw.dmzc.window.DmzcjlAddWindow;
import org.jetbrains.annotations.NotNull;


public final class ZcjlAddByCodeAction extends AnAction {
    public ZcjlAddByCodeAction(String text) {
        super(text);
    }

    public ZcjlAddByCodeAction() {
        this("选中代码");
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = AnAction.getEventProject(e);
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        if (editor == null) {
            Notification notification = NotificationUtil.create("编辑器没有打开", "代码走查", NotificationType.ERROR);
            notification.notify(project);
        } else {
            ImageBuilder imageBuilder = new ImageBuilder(editor);
            long selectedSize = imageBuilder.getSelectedSize();
            if (selectedSize == 0L) {
                Notification notification = NotificationUtil.create("选中的编辑器中没有选中文本", "代码走查", NotificationType.ERROR);
                notification.notify(project);
            } else {
                int startLineNum = imageBuilder.getStartLineNum();
                int endLineNum = imageBuilder.getEndLineNum();
                byte[] tpBytes = imageBuilder.createImage();
                if (tpBytes == null) {
                    Notification notification = NotificationUtil.create("图片创建失败！", "代码走查", NotificationType.ERROR);
                    notification.notify(project);
                } else {
                    DmzcjlAddWindow.open(e.getProject(), tpBytes, startLineNum, endLineNum);
//                    TestDialogWrapper testDialogWrapper = new TestDialogWrapper(e.getProject());
//                    testDialogWrapper.show();
                }
            }
        }
    }

}
